using Gateway.Domain.Entities;
using Gateway.IApplication.Order;
using Gateway.IRepositories;
using Microsoft.EntityFrameworkCore;

namespace Gateway.Application;
/// <summary>
/// 
/// </summary>
public class OrderApp : IOrderApp
{
    private readonly IAppRepository<Order> _repository;
    private readonly IUnitOfWork _unitOfWork;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="repository"></param>
    /// <param name="unitOfWork"></param>
    public OrderApp(IAppRepository<Order> repository, IUnitOfWork unitOfWork)
    {
        _repository = repository;
        _unitOfWork = unitOfWork;
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="orderTime"></param>
    /// <returns></returns>
    public async Task<string> Add(DateTime orderTime)
    {
        var order = new Order();
        order.OrderStatus = "NoPay";
        order.Area = "郑州";
        order.Money = 1000;
        order.Payer = Guid.NewGuid().ToString();
        order.OrderTime = orderTime;
        await _repository.AddAsync(order);
        await _unitOfWork.CommitAsync();
        return order.Id;
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task<object> GetDetail(string id)
    {
        return await _repository.FirstOrDefaultAsync(x => x.Id.Equals(id));
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="area"></param>
    /// <param name="page"></param>
    /// <param name="size"></param>
    /// <returns></returns>
    public async Task<object> GetList(string area, int page, int size)
    {
        return await _repository.Entities.Where(x => string.IsNullOrEmpty(area)?true: x.Area.Equals(area) ).OrderByDescending(x=>x.Id).Skip(page).Take(size).ToListAsync();
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="area"></param>
    /// <returns></returns>
    public async Task<object> GetList(string area)
    {
        return await _repository.GetListAsync(x => string.IsNullOrEmpty(area)?true: x.Area.Equals(area) );
    }
}